home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / getrusag.c < prev    next >
C/C++ Source or Header  |  1991-11-16  |  1KB  |  66 lines

  1. /* getrusage emulation for MiNT */
  2.  
  3. #include <osbind.h>
  4. #include <mintbind.h>
  5. #include <time.h>
  6. #include <resource.h>
  7. #include <errno.h>
  8.  
  9. extern int __mint;
  10. extern long _childtime;
  11.  
  12. void
  13. _ms2tval(milliseconds, tval)
  14.     unsigned long milliseconds;
  15.     struct timeval *tval;
  16. {
  17.     tval->tv_sec = milliseconds/1000;
  18.     tval->tv_usec = (milliseconds % 1000) * 1000;
  19. }
  20.  
  21. void
  22. _add_tval(orig, new)
  23.     struct timeval *orig, *new;
  24. {
  25.     long t;
  26.  
  27.     t = orig->tv_usec + new->tv_usec;
  28.     if (t > 1000000) {
  29.         orig->tv_sec += t/1000000;
  30.         t = t % 1000000;
  31.     }
  32.     orig->tv_usec = t;
  33.     orig->tv_sec += new->tv_sec;
  34. }
  35.  
  36. int
  37. getrusage(which, data)
  38.     int which;
  39.     struct rusage *data;
  40. {
  41.     long r;
  42.     long usage[8];
  43.  
  44.     if (__mint) {
  45.         r = Prusage(usage);
  46.         if (r < 0) {
  47.             errno = -r;
  48.             return -1;
  49.         }
  50.     } else {
  51.         usage[0] = usage[2] = 0;
  52.         usage[1] = clock() - _childtime;
  53.         usage[3] = _childtime;
  54.     }
  55.  
  56.     if (which == RUSAGE_SELF) {
  57.         _ms2tval(usage[0], &(data->ru_stime));
  58.         _ms2tval(usage[1], &(data->ru_utime));
  59.     }
  60.     else if (which == RUSAGE_CHILDREN) {
  61.         _ms2tval(usage[2], &(data->ru_stime));
  62.         _ms2tval(usage[3], &(data->ru_utime));
  63.     }
  64.     return 0;
  65. }
  66.